package com.example.demo;


import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;


/**
 * @author 陌大神 E-mail:
 * @version 创建时间：2020年12月31日 下午4:56:15
 * 用户名参数化
 * 类说明
 */
public class GithubTestCases {
    WebDriver driver;

    List<String>repositories_names=new ArrayList<String>();

    @BeforeClass
    public void initDriver() {
//        C:\Users\Administrator>where chromedriver
//        C:\Users\Administrator\AppData\Roaming\npm\chromedriver.cmd
//        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Administrator\\AppData\\Roaming\\npm\\chromedriver.cmd");
        driver=new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://github.com/login");
        //10s内轮训查找5s中找到就继续下一个，只针对findElement--隐试等待
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }

    @Test //加@Test注解就会按test类执行
    public void test001_login() throws Exception {

        //String TakesScreenshot;
        //driver.findElement(By.partialLinkText("sign-in")).click();
        try {
            driver.findElement(By.id("login_field")).sendKeys("enmin@sina.cn");

            driver.findElement(By.id("password")).sendKeys("Winnermin123");
            driver.findElement(By.name("commit")).click();//commit是name定位

            Thread.sleep(5000);
            //getPageSource获取整个页面源码，
//				if(driver.getPageSource().contains("Learn Git and GitHub without any code!")){
//					System.out.println("登录成功");
//				}else{
//					System.out.println("登录失败");
//				}

            //断言
            Assert.assertEquals(driver.getPageSource().contains("Learn Git and GitHub without any code!"), true);

        }catch (Exception |Error e) {
            File file=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(file, new File("images/testfanloginFailure.png"));
            //Reporter.log("<a href=../images/testfanloginfailure.png target=_blan>");
        }

    }

    @Test (dependsOnMethods="test001_login")
    public void test002_create() throws Exception{

        //获取现有的
        driver.get("https://github.com/enmin0522?tab=repositories");
        //拿到所以元素的文本
        List<WebElement> repositories= driver.findElements(By.cssSelector("a[itemprop='name codeRepository']"));//a标签使用
        //List<String>repositories_names=new ArrayList<String>();
        for(WebElement w : repositories) {
            repositories_names.add(w.getText().trim());
        }

        driver.findElement(By.className("octicon-plus")).click();
        driver.findElement(By.partialLinkText("New repository")).click(); //a标签是超链接，用部分超链接文本partialLinkText

        //随机生成一个响应库
        String name="";
        int i=100;
        while(i>0) {
            int j=(int)(Math.random()*100);
            name="test006"+j;
            if(repositories_names.contains(name)) {
                i--;
            }else {
                break;
            }
        }

        driver.findElement(By.id("repository_name")).sendKeys("name");
        //driver.findElement(By.className("btn btn-primary first-in-line")).click();//第一次执行报错了
        driver.findElement(By.cssSelector(".btn-primary.first-in-line")).click();
        String url="https://github.com/enmin0522/"+name;
        Thread.sleep(5000);
        String currentUrl=driver.getCurrentUrl();//获取系统登录的url地址
        //判断预期url与获取url是否一致
        //字符串的对比用equals
        if(url.equals(currentUrl)){
            System.out.println("创建respostiry成功");
        }else{
            System.out.println("创建respostiry失败");
        }
    }

    @Test  (dependsOnMethods="test001_login")
    public void test003_change() throws Exception {
        //随机生成一个响应库
        String name="";
        int i=100;
        while(i>0) {
            int j=(int)(Math.random()*100);
            name="test006"+j;
            if(repositories_names.contains(name)) {
                i--;
            }else {
                break;
            }
        }

        driver.get("https://github.com/");//从首页进去的
        driver.findElement(By.className("dashboard-underlined-link")).click();
        driver.findElement(By.partialLinkText("Settings")).click();
        //--显示等待，元素不可用也可以使用
        WebDriverWait wait=new WebDriverWait(driver,20);
        wait.until(ExpectedConditions.presenceOfElementLocated(By.name("new_name")));
        //--显示等待，元素不可用等元素出现再用
        //wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("new_name")));
        driver.findElement(By.name("new_name")).clear();


        int k=(int) (Math.random()*repositories_names.size());

        //对仓库名称重复的判断
        driver.findElement(By.name("new_name")).sendKeys(repositories_names.get(k));//已存在的用户名去变量
        WebElement error=driver.findElement(By.className("error"));
        String text =error.getText();
        //提示语也是变化的
        if(text.equals("The repository "+repositories_names.get(k)+"enmin0522 already exists on this account")){
            System.out.println("重复验证提示正确");
        }else{
            System.out.println("重复验证提示失败");
        }
        driver.findElement(By.name("new_name")).sendKeys(name);
        driver.findElement(By.className("flex-self-end")).click();
        Thread.sleep(3000);
        String title=driver.getTitle();
        if(title.equals("enmin0522/"+name)) {
            System.out.println("修改正确");
        }else {
            System.out.println("修改失败");
        }
    }

    @AfterClass
    public void quit() {
        driver.quit();
    }
}
